package entity

import (
	"time"
	"xr-game-server/core/constants/db"
	"xr-game-server/core/migrate"
	"xr-game-server/core/snowflake"
	"xr-game-server/core/syndb"
)

const (
	// TbItemLog 表名
	TbItemLog db.TbName = "item_logs"
)

// 列名
const (
	ItemLogOld      db.TbCol = "old"
	ItemLogCurrent  db.TbCol = "current"
	ItemLogFail     db.TbCol = "fail"
	ItemLogSourceId db.TbCol = "source_id"
	ItemLogRoleId   db.TbCol = "role_id"
	ItemLogVal      db.TbCol = "val"
	ItemLogItemId   db.TbCol = "item_id"
	ItemLogTypeId   db.TbCol = "type_id"
)

type ItemLog struct {
	migrate.OneModel
	TypeId   uint32 `gorm:"default:0;comment:数据变化类型0加 1减"`
	Old      uint64 `gorm:"default:0;comment:旧数据"`
	Current  uint64 `gorm:"default:0;comment:变更后数据"`
	Fail     uint64 `gorm:"default:0;comment:失败数据"`
	SourceId uint64 `gorm:"default:0;comment:数据源id"`
	RoleId   uint64 `gorm:"default:0;comment:角色id"`
	Val      uint64 `gorm:"default:0;comment:变更值"`
	ItemId   uint64 `gorm:"default:0;comment:物品id"`
}

func initItemLog() {
	syndb.RegLazyWithLarge(TbItemLog, ItemLogOld)
	syndb.RegLazyWithLarge(TbItemLog, ItemLogCurrent)
	syndb.RegLazyWithLarge(TbItemLog, ItemLogFail)
	syndb.RegLazyWithLarge(TbItemLog, ItemLogSourceId)
	syndb.RegLazyWithLarge(TbItemLog, ItemLogRoleId)
	syndb.RegLazyWithLarge(TbItemLog, ItemLogVal)
	syndb.RegLazyWithLarge(TbItemLog, ItemLogItemId)
	syndb.RegLazyWithLarge(TbItemLog, ItemLogTypeId)

	syndb.RegLazyWithLarge(TbItemLog, db.CreatedAtName)
	syndb.RegLazyWithLarge(TbItemLog, db.UpdatedAtName)

	migrate.AutoMigrate(&ItemLog{})
}

func (receiver *ItemLog) SetTypeId(typeId uint32) {
	receiver.TypeId = typeId
	syndb.AddDataToLazyChan(TbItemLog, ItemLogTypeId, &syndb.ColData{
		IdVal:  receiver.ID,
		ColVal: typeId,
	})
}

func (receiver *ItemLog) SetVal(val uint64) {
	receiver.Val = val
	syndb.AddDataToLazyChan(TbItemLog, ItemLogVal, &syndb.ColData{
		IdVal:  receiver.ID,
		ColVal: val,
	})
}

func (receiver *ItemLog) SetItemId(itemId uint64) {
	receiver.ItemId = itemId
	syndb.AddDataToLazyChan(TbItemLog, ItemLogItemId, &syndb.ColData{
		IdVal:  receiver.ID,
		ColVal: itemId,
	})
}
func (receiver *ItemLog) SetRoleId(roleId uint64) {
	receiver.RoleId = roleId
	syndb.AddDataToLazyChan(TbItemLog, ItemLogRoleId, &syndb.ColData{
		IdVal:  receiver.ID,
		ColVal: roleId,
	})
}
func (receiver *ItemLog) SetOld(old uint64) {
	receiver.Old = old
	syndb.AddDataToLazyChan(TbItemLog, ItemLogOld, &syndb.ColData{
		IdVal:  receiver.ID,
		ColVal: old,
	})
}
func (receiver *ItemLog) SetCurrent(current uint64) {
	receiver.Current = current
	syndb.AddDataToLazyChan(TbItemLog, ItemLogCurrent, &syndb.ColData{
		IdVal:  receiver.ID,
		ColVal: current,
	})
}
func (receiver *ItemLog) SetFail(fail uint64) {
	receiver.Fail = fail
	syndb.AddDataToLazyChan(TbItemLog, ItemLogFail, &syndb.ColData{
		IdVal:  receiver.ID,
		ColVal: fail,
	})
}
func (receiver *ItemLog) SetSourceId(sourceId uint64) {
	receiver.SourceId = sourceId
	syndb.AddDataToLazyChan(TbItemLog, ItemLogSourceId, &syndb.ColData{
		IdVal:  receiver.ID,
		ColVal: sourceId,
	})
}
func (receiver *ItemLog) SetCreatedAt(val time.Time) {
	receiver.CreatedAt = val
	syndb.AddDataToLazyChan(TbItemLog, db.CreatedAtName, &syndb.ColData{
		IdVal:  receiver.ID,
		ColVal: val,
	})
}

func (receiver *ItemLog) SetUpdatedAt(val time.Time) {
	receiver.UpdatedAt = val
	syndb.AddDataToLazyChan(TbItemLog, db.UpdatedAtName, &syndb.ColData{
		IdVal:  receiver.ID,
		ColVal: val,
	})
}

func NewItemLog() *ItemLog {
	itemLog := &ItemLog{}
	itemLog.ID = snowflake.GetId()
	itemLog.SetUpdatedAt(time.Now())
	itemLog.SetCreatedAt(time.Now())
	return itemLog
}
